package com.xxx.service.user.impl;

import com.xxx.common.enums.LoginType;
import com.xxx.common.enums.SmsType;
import com.xxx.common.enums.StatusEnum;
import com.xxx.common.model.PageInfo;
import com.xxx.dao.UserBaseInfoDao;
import com.xxx.date.DateUtil;
import com.xxx.common.table.UserBaseInfoEntity;
import com.xxx.model.user.req.ReqGetUserDetailsVO;
import com.xxx.model.user.req.ReqLoginVo;
import com.xxx.model.user.req.ReqUpdateUserInfoVO;
import com.xxx.model.user.req.ReqUserListVO;
import com.xxx.model.user.res.ResGetUserDetailsVO;
import com.xxx.model.user.res.ResLoginVo;
import com.xxx.model.user.res.ResRefreshTokenVo;
import com.xxx.model.user.res.ResUserListVO;
import com.xxx.redis.StringRedisUtil;
import com.xxx.service.user.UserCommonService;
import com.xxx.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Flying
 * @Description
 * @date 14:42 2018-08-24
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserBaseInfoDao userBaseInfoDao;

    @Autowired
    private StringRedisUtil redisUtil;

    @Autowired
    private UserCommonService userCommonService;

    /**
     * 用户登陆
     *
     * @param reqLoginVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResLoginVo login(ReqLoginVo reqLoginVo) {


        userCommonService.checkSmsCode(reqLoginVo.getPhone(),reqLoginVo.getCaptcha(),SmsType.LOGIN_REGISTER_SMS);

        UserBaseInfoEntity userBaseInfoEntity = userBaseInfoDao.selectByUserName(reqLoginVo.getPhone());

        if(userBaseInfoEntity == null){
            userBaseInfoEntity = new UserBaseInfoEntity();
            userBaseInfoEntity.setUserName(reqLoginVo.getPhone());
            userBaseInfoEntity.setPhone(reqLoginVo.getPhone());
            userBaseInfoEntity.setCreateTime(DateUtil.nowFormatDate());
            userBaseInfoEntity.setStatus(StatusEnum.NORMAL.getValue());
            userBaseInfoEntity.setNickName(reqLoginVo.getPhone());
            this.createUser(userBaseInfoEntity);
        }

        ResRefreshTokenVo resRefreshTokenVo = userCommonService.createToken(userBaseInfoEntity.getIdUserBaseInfo().toString(), LoginType.NAPP);
        ResLoginVo resLoginVo =  getUserInfo(userBaseInfoEntity.getIdUserBaseInfo());
        resLoginVo.setRefreshToken(resRefreshTokenVo.getRefreshToken());
        resLoginVo.setAccessToken(resRefreshTokenVo.getAccessToken());
        return resLoginVo;
    }


    @Override
    public ResLoginVo getUserInfo(Long userId){
        UserBaseInfoEntity userBaseInfoEntity = userBaseInfoDao.selectById(userId);
        ResLoginVo resLoginVo = new ResLoginVo();
        resLoginVo.setUserName(userBaseInfoEntity.getUserName());
        resLoginVo.setPhone(userBaseInfoEntity.getPhone());
        resLoginVo.setNickName(userBaseInfoEntity.getNickName());
        resLoginVo.setIdxCityAreaInfo(userBaseInfoEntity.getIdxCityAreaInfo());
        resLoginVo.setIdxCountyAreaInfo(userBaseInfoEntity.getIdxCountyAreaInfo());
        resLoginVo.setIdxProvinceAreaInfo(userBaseInfoEntity.getIdxProvinceAreaInfo());
        resLoginVo.setGender(userBaseInfoEntity.getGender());
        resLoginVo.setBirthday(userBaseInfoEntity.getBirthday());
        resLoginVo.setHeadImagePath(userBaseInfoEntity.getHeadImagePath());
        return resLoginVo;
    }

    /**
     * 获取用户信息
     *
     * @param phone@return
     */
    @Override
    public ResLoginVo getUserInfo(String phone) {
        UserBaseInfoEntity userBaseInfoEntity = userBaseInfoDao.selectByUserName(phone);
        ResLoginVo resLoginVo = new ResLoginVo();
        resLoginVo.setUserBaseInfoId(userBaseInfoEntity.getIdUserBaseInfo());
        resLoginVo.setUserName(userBaseInfoEntity.getUserName());
        resLoginVo.setPhone(userBaseInfoEntity.getPhone());
        resLoginVo.setNickName(userBaseInfoEntity.getNickName());
        resLoginVo.setIdxCityAreaInfo(userBaseInfoEntity.getIdxCityAreaInfo());
        resLoginVo.setIdxCountyAreaInfo(userBaseInfoEntity.getIdxCountyAreaInfo());
        resLoginVo.setIdxProvinceAreaInfo(userBaseInfoEntity.getIdxProvinceAreaInfo());
        resLoginVo.setGender(userBaseInfoEntity.getGender());
        resLoginVo.setBirthday(userBaseInfoEntity.getBirthday());
        resLoginVo.setHeadImagePath(userBaseInfoEntity.getHeadImagePath());
        return resLoginVo;
    }

    /**
     * description:修改用户信息
     *
     * @param reqUpdateUserInfoVO
     * @return
     * @author: Flying
     * @date: 2018-08-27 18:48
     * @param: null
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateUserInfo(ReqUpdateUserInfoVO reqUpdateUserInfoVO) {
        UserBaseInfoEntity userBaseInfoEntity = new UserBaseInfoEntity();
        userBaseInfoEntity.setBirthday(reqUpdateUserInfoVO.getBirthday());
        userBaseInfoEntity.setNickName(reqUpdateUserInfoVO.getNickName());
        userBaseInfoEntity.setGender(reqUpdateUserInfoVO.getGender());
        userBaseInfoEntity.setIdUserBaseInfo(reqUpdateUserInfoVO.getUserId());
        userBaseInfoEntity.setIdxProvinceAreaInfo(reqUpdateUserInfoVO.getProvinceAreaId());
        userBaseInfoEntity.setIdxCityAreaInfo(reqUpdateUserInfoVO.getCityArealId());
        userBaseInfoEntity.setIdxCountyAreaInfo(reqUpdateUserInfoVO.getCountyArealId());
        userBaseInfoEntity.setHeadImagePath(reqUpdateUserInfoVO.getHeadImagePath());
        return userBaseInfoDao.updateById(userBaseInfoEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserBaseInfoEntity createUser(UserBaseInfoEntity userBaseInfoEntity){
        userBaseInfoDao.insert(userBaseInfoEntity);
        return userBaseInfoEntity;
    }

    /**
     * @param reqUserListVO
     * @return com.xxx.common.model.PageInfo<com.xxx.model.operation.res.ResUserListVO>
     * @Author Flying
     * @Description 获取用户列表
     * @Date 14:19 2019/4/11
     * @Param [reqUserListVO]
     */
    @Override
    public PageInfo<ResUserListVO> getUserList(ReqUserListVO reqUserListVO) {
        PageInfo<ResUserListVO> pageInfo = new PageInfo<>(reqUserListVO);
        List<ResUserListVO> list = userBaseInfoDao.getUserList(pageInfo,reqUserListVO);
        Integer count = userBaseInfoDao.getUserListTotalCount(reqUserListVO);
        pageInfo.setData(list);
        pageInfo.setTotalResult(count);
        return pageInfo;
    }

    /**
     * @param reqGetUserDetailsVO
     * @return com.xxx.model.operation.res.ResGetUserDetailsVO
     * @Author Flying
     * @Description 获取用户详情
     * @Date 16:12 2019/4/11
     * @Param [reqGetUserDetailsVO]
     */
    @Override
    public ResGetUserDetailsVO getUserDetails(ReqGetUserDetailsVO reqGetUserDetailsVO) {

        ResGetUserDetailsVO resGetUserDetailsVO = new ResGetUserDetailsVO();
        UserBaseInfoEntity userBaseInfoEntity = userBaseInfoDao.selectById(reqGetUserDetailsVO.getUserBaseId());
        resGetUserDetailsVO.setUserName(userBaseInfoEntity.getUserName());
        resGetUserDetailsVO.setNickName(userBaseInfoEntity.getNickName());
        resGetUserDetailsVO.setGender(userBaseInfoEntity.getGender());
        resGetUserDetailsVO.setHeadImagePath(userBaseInfoEntity.getHeadImagePath());
        resGetUserDetailsVO.setPhone(userBaseInfoEntity.getPhone());
        resGetUserDetailsVO.setStatus(StatusEnum.valueOf(userBaseInfoEntity.getStatus()));
        resGetUserDetailsVO.setCreateTime(userBaseInfoEntity.getCreateTime());
        return resGetUserDetailsVO;
    }
}
